package com.qingxin.park.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.ParkTeSe;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.FarmParkTeseService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.utils.StringUtils;

/**
 * 园区
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/park")
public class FarmParkAction extends BaseAction {
	
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private SysGroupService sysGroupService;
	
	@Autowired
	private FarmParkTeseService farmParkTeseService;
	
	/**
	 * 获取指定用户关联的园区数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getParkData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getParkData(HttpServletRequest request){
		return farmParkService.getParkData(getLoginUser(request));
	}
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/park/park_list";
	}
	
	
	/**
	 * 获取列表数据
	 * @param parkName
	 * @param parkStatus
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String parkName, String parkStatus, PageParam pageParam, HttpServletRequest request){
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		return farmParkService.list(groupid, parkName, parkStatus, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加园区信息
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String datatype, String leftdata, HttpServletRequest request){
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			model.put("groupid", leftdata);
			
			SysGroup group = sysGroupService.get(leftdata);
			if(group.getGrouptype() != 3){
				model.put("errormessage", "该分组【"+group.getGroupname()+"】不是一个园区类型分组，不能在该分组下创建园区信息");
				return "sys/error/error";
			}
			
			FarmPark park = farmParkService.getByProperty("groupid", leftdata);
			if(park != null){
				model.put("errormessage", "该分组【"+group.getGroupname()+"】已存在园区信息，不能在该分组下重复创建园区信息");
				return "sys/error/error";
			}
			
		}else{
			model.put("groupid", "");
		}
		return "trace/park/park_add";
	}
	
	
	/**
	 * 保存园区信息
	 * @param park
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid FarmPark park, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		String parkMax = farmParkService.getParkNoMax(park.getCompanyid());
		if(StringUtils.isNotEmpty(parkMax)){
			int number = Integer.parseInt(parkMax)+1;
			String maxNo = number+"";
			if(maxNo.length()==1){
				maxNo = "0"+maxNo;
			}
			park.setParkNo(maxNo);
		}else{
			park.setParkNo("01");
		}
		
		SysError info = farmParkService.add(park, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改园区信息
	 * @param model
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String parkid){
		FarmPark park = farmParkService.get(parkid);
		model.put("park", park);
		
		if(park != null){
			Company company = companyService.get(park.getCompanyid());
			model.put("company", company);
		}
		
		return "trace/park/park_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param park
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid FarmPark park, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = farmParkService.update(park, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 修改园区介绍
	 * @param model
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/editDescrib.do", method=RequestMethod.GET)
	public String editDescrib(ModelMap model, String parkid){
		FarmPark park = farmParkService.get(parkid);
		model.put("park", park);
		
		return "trace/park/park_editDescrib";
	}
	
	
	/**
	 * 修改园区介绍
	 * @param parkid
	 * @param describ
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateDescrib.json", method=RequestMethod.POST)
	@ResponseBody
	public Object updateDescrib(String parkid, String describ, HttpServletRequest request){
		SysError info = farmParkService.updateDescrib(parkid, describ, getLoginUser(request));
		return info;
	}
	
	
	/**
	 * 查看园区详情
	 * @param model
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String parkid){
		FarmPark park = farmParkService.get(parkid);
		model.put("park", park);
		
		if(park != null){
			Company company = companyService.get(park.getCompanyid());
			model.put("company", company);
		}
		
		return "trace/park/park_view";
	}
	
	/**
	 * 修改园区特色属性
	 * @param parkid
	 * @param teseStatus
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/teseOKorNO.json", method=RequestMethod.POST)
	@ResponseBody
	public Object teseOKorNO(String[] parkid,String teseStatus, HttpServletRequest request){
		if(parkid==null || parkid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return farmParkService.updateTese(parkid,teseStatus, getLoginUser(request), request);
	}
	
	/**
	 * 删除园区信息
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] parkid, HttpServletRequest request){
		if(parkid==null || parkid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return farmParkService.delete(parkid, getLoginUser(request), request);
	}
	
	
	
	/**
	 * 判断园区名是否存在
	 * @param parkName
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String parkName, String parkid){
		boolean existed = farmParkService.isNameDuplicate(parkName, parkid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断园区编码是否存在
	 * @param parkNo
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNoDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNoDuplicate(String parkNo, String parkid){
		boolean existed = farmParkService.isNoDuplicate(parkNo, parkid);
		return existed ? "1":"0";
	}
	
	/**
	 * 特色基地列表页
	 * @param model
	 * @param tstype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listtese.do", method=RequestMethod.GET)
	public String list(ModelMap model,String tstype, HttpServletRequest request){
		model.put("tstype", tstype);
		return "trace/park/tese_list";
	}
	
	@RequestMapping(value="/listdataTese.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdataTese(String tstype, String leftdata, String title,PageParam pageParam, HttpServletRequest request){
		
		return farmParkTeseService.list(tstype,leftdata, title,pageParam, getLoginUser(request));
	}
	
	/**
	 * 添加特色基地
	 * @param model
	 * @param tstype
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addTese.do", method=RequestMethod.GET)
	public String addTese(ModelMap model,String tstype, String leftdata, HttpServletRequest request){
		model.put("parkid", leftdata);
		model.put("tstype", tstype);
		return "/trace/park/tese_add";
	}
	
	/**
	 * 保存特色基地
	 * @param parkTeSe
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/saveTese.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid ParkTeSe parkTeSe ,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = farmParkTeseService.add(parkTeSe,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除特色基地
	 * @param parktsid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/deleteTese.json", method=RequestMethod.POST)
	@ResponseBody
	public Object deleteTese(String[] parktsid, HttpServletRequest request){
		return farmParkTeseService.delete(parktsid, request);
	}
	
	/**
	 * 修改特色基地
	 * @param model
	 * @param articleId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editTese.do", method=RequestMethod.GET)
	public String editTese(ModelMap model, String parktsid, HttpServletRequest request){
		ParkTeSe parkTeSe = farmParkTeseService.get(parktsid);
		model.put("parkTeSe", parkTeSe);
		return "trace/park/tese_edit";
	}
	
	/**
	 * 保存修改
	 * @param article
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateTese.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid ParkTeSe parkTeSe, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return farmParkTeseService.update(parkTeSe, getLoginUser(request), request);
	}
	

}
